/**
 * 封装2个文件模块常用的表单字段，即提交信息和目标分支
 */

import React from 'react';
import PropTypes from 'prop-types';
import { Form, Input } from '@teamix/ui';
import { getFeature } from 'utils/features';
import intl from '../../locale';

const { resourceSubject } = window.FORCE;

export default class ProjectTab extends React.Component {
  static propTypes = {
    defaultCommitMsg: PropTypes.string,
    defaultCommitMsgDesc: PropTypes.string,
  };

  static defaultProps = {
    defaultCommitMsg: 'overview',
  };

  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.props.defaultCommitMsg !== nextProps.defaultCommitMsg) {
      nextProps.field.setValue('commit_message_title', nextProps.defaultCommitMsg);
    }
  }

  getCommitMessagePlaceholder = () => {
    if (getFeature('project.associated_projex_workitem')) {
      return intl.get({
        id: 'code-assets.components.fileBasicForm.EnterANoteYouCan',
        defaultMessage:
          '请输入备注信息，使用 #ID 可以快速关联任务，如 #TOY-60 将链接到 ID 为 TOY-60 的任务',
      });
    }
    return '请输入提交说明';
  };

  render() {
    const { defaultCommitMsg, defaultCommitMsgDesc, field, label } = this.props;
    const { init } = field;

    return (
      <Form labelAlign="top" direction="ver" field={field} className="commit-message-form">
        <Form.Item label={label} required style={{ marginBottom: 12 }}>
          <Input
            autoFocus
            placeholder={defaultCommitMsg}
            {...init('commit_message_title', {
              initValue: defaultCommitMsg,
              rules: [
                {
                  required: true,
                  message: intl.get({
                    id: 'code-assets.src.utils.locale.ThisParameterIsRequired',
                    defaultMessage: '该项为必填项',
                  }),
                },
              ],
            })}
          />
        </Form.Item>
        <Input
          style={{ display: 'none' }}
          {...init('branch_name', {
            initValue: resourceSubject.revision || 'master',
            rules: [
              {
                required: true,
                message: intl.get({
                  id: 'code-assets.src.utils.locale.ThisParameterIsRequired',
                  defaultMessage: '该项为必填项',
                }),
              },
            ],
          })}
        />

        <Form.Item>
          <Input.TextArea
            placeholder={this.getCommitMessagePlaceholder()}
            {...init('commit_message_content', {
              initValue: defaultCommitMsgDesc,
            })}
          />
        </Form.Item>
        {/* 目标分支字段暂不开放 */}
        {/*
           <Form.Item label={locale['file_target_branch']} required="required" {...formItemLayout}>
             <Input
               {...init('branch_name', {
                 initValue: resourceSubject.revision,
                 rules: [{ required: true, message: locale['required'] }],
               })}
             />
           </Form.Item>
          */}
      </Form>
    );
  }
}
